package com.dingreading.cloud.admin.service.impl;

import com.dingreading.cloud.admin.dto.CourseTrainLogDto;
import com.dingreading.cloud.admin.entity.CourseTrainLog;
import com.dingreading.cloud.admin.entity.table.CourseTrainLogTableDef;
import com.dingreading.cloud.admin.mapper.CourseTrainLogMapper;
import com.dingreading.cloud.admin.service.CourseTrainLogService;
import com.dingreading.cloud.common.util.QueryFiledUtil;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2025-11-20
 */
@Service
public class CourseTrainLogServiceImpl extends BaseServiceImpl<CourseTrainLogMapper, CourseTrainLog, CourseTrainLogTableDef> implements CourseTrainLogService {

    @Override
    protected CourseTrainLogTableDef getTable() {
        return CourseTrainLogTableDef.courseTrainLog;
    }

    @Override
    public List<CourseTrainLogDto> getAggregatedLogs(String storeUid, String beginDate, String endDate) {
        QueryCondition condition = QueryCondition.createEmpty()
                .and(table.storeUid.eq(storeUid))
                .and(table.addTime.between(QueryFiledUtil.appendMin00(beginDate), QueryFiledUtil.appendMin24(endDate)));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition);
        List<CourseTrainLogDto> courseTrainLogs = getMapper().selectListWithRelationsByQueryAs(wrapper, CourseTrainLogDto.class);

        if (courseTrainLogs.isEmpty()) {
            return new ArrayList<>();
        }

        Map<String, CourseTrainLogDto> aggregatedMap = new HashMap<>();
        for (CourseTrainLogDto courseTrainLog : courseTrainLogs) {
            // 创建一个唯一标识符作为 Map 的 key
            String key = courseTrainLog.getCorpUid() + "::" + courseTrainLog.getStoreUid() + "::" +
                    courseTrainLog.getStaffUid() + "::" + courseTrainLog.getType() + "::" +
                    courseTrainLog.getCourseTrainUid() + "::" + courseTrainLog.getFileUid();

            // 从 map 中获取 DTO，如果不存在，则创建一个新的
            CourseTrainLogDto dto = aggregatedMap.get(key);

            if (dto == null) {
                // 首次遇到这个分组，创建新的 DTO 并放入 map
                dto = new CourseTrainLogDto();
                dto.setCorpUid(courseTrainLog.getCorpUid());
                dto.setStoreUid(courseTrainLog.getStoreUid());
                dto.setStaffUid(courseTrainLog.getStaffUid());
                dto.setType(courseTrainLog.getType());
                dto.setCourseTrainUid(courseTrainLog.getCourseTrainUid());
                dto.setFileUid(courseTrainLog.getFileUid());

                dto.setRecordNum(1L); // 计数从1开始
                aggregatedMap.put(key, dto);
            } else {
                // 该分组已存在，只需将浏览次数加1
                dto.setRecordNum(dto.getRecordNum() + 1);
            }
            if (StringUtils.isNotBlank(courseTrainLog.getLevelName()))
                dto.setLevelName(courseTrainLog.getLevelName());
            if (StringUtils.isNotBlank(courseTrainLog.getAddStaffName()))
                dto.setStaffName(courseTrainLog.getAddStaffName());
            if (StringUtils.isNotBlank(courseTrainLog.getUsername()))
                dto.setUsername(courseTrainLog.getUsername());
            if (StringUtils.isNotBlank(courseTrainLog.getCourseTrainName()))
                dto.setCourseTrainName(courseTrainLog.getCourseTrainName());
            if (StringUtils.isNotBlank(courseTrainLog.getStoreName()))
                dto.setStoreName(courseTrainLog.getStoreName());

        }

        List<CourseTrainLogDto> resultList = new ArrayList<>(aggregatedMap.values());
        return resultList;
    }

    @Override
    public List<String> getStores(String beginDate, String endDate) {
        QueryCondition condition = QueryCondition.createEmpty()
                .and(table.addTime.between(QueryFiledUtil.appendMin00(beginDate), QueryFiledUtil.appendMin24(endDate)));

        QueryWrapper wrapper = QueryWrapper.create()
                .select(table.storeUid)
                .where(condition);
        return getMapper().selectObjectListByQueryAs(wrapper, String.class);
    }
}
